package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.ec.module.vo.ChgFormVO;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.entity.RtChangeLockInfoDO;
import cqrtplm.entity.RtChangeaffectAssociatedbomDO;
import cqrtplm.entity.RtChangeaffectFinishedbomDO;
import cqrtplm.mapper.RtChangeLockInfoMapper;
import cqrtplm.mapper.RtChangeaffectAssociatedbomMapper;
import cqrtplm.mapper.RtChangeaffectFinishedbomMapper;
import cqrtplm.service.RTUnlockProductOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class RTUnlockProductOrderImpl implements RTUnlockProductOrderService {
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    private RtChangeLockInfoMapper rtChangeLockInfoMapper;
    @Resource
    private RtChangeaffectFinishedbomMapper finishedbomMapper;
    @Resource
    private RtChangeaffectAssociatedbomMapper associatedbomMapper;

    /**
     * 解锁成品码
     *
     * @param workFlowContainerId@return
     */
    @Override
    public void unlockProductOrder(String workFlowContainerId, String isLock) {
        try {
            WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(new BigInteger(workFlowContainerId));
            if (!Objects.isNull(workFlowContainerView) && workFlowContainerView instanceof FlowChgFormView) {
                log.info("当前流程属于变更通告流程");
                FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
                //获取变更通告对象
                ChgFormVO chgFormVO = flowChgFormView.getChgFormVO();
                String changeNo = chgFormVO.getObjectNumber();
                String formData = chgFormVO.getFormData();
                JSONObject testObj = JSONObject.parseObject(formData);
                if (StringUtils.isNotBlank(changeNo)) {
                    if (changeNo.contains("RTTJST")) {
                        //锁定订单
                        String ZYDJ = testObj.getString("ZYDJ");//重要等级
                        if (ZYDJ.contains("A级")) {
                            if ("2".equals(isLock)) {
                                UserDO user = TyAccountContext.getUser();
                                List<RtChangeaffectFinishedbomDO> finishedbomDOS = finishedbomMapper.queryByChangeNoAndPartNo(changeNo, "");
                                List<RtChangeaffectAssociatedbomDO> associatedbomDOS = associatedbomMapper.queryByChangeNoAndPartNo(changeNo, "");
                                List<RtChangeLockInfoDO> lockInfoDOS =new ArrayList<>();
                                if (CollUtil.isNotEmpty(finishedbomDOS)) {
                                    for (RtChangeaffectFinishedbomDO finishedbomDO : finishedbomDOS) {
                                        String partNumber = finishedbomDO.getPartnumber();
                                        String productCode = finishedbomDO.getFinishedproductcode();
                                        String materialCode = finishedbomDO.getMaterialcode();
                                        RtChangeLockInfoDO linkInfo = new RtChangeLockInfoDO();
                                        linkInfo.setProductCode(productCode);
                                        linkInfo.setChangeNo(changeNo);
                                        linkInfo.setPartNumber(partNumber);
                                        linkInfo.setMaterialCode(materialCode);
                                        linkInfo.setLocker(user.getDisplayName());
                                        linkInfo.setLockTime(new Date());
                                        linkInfo.setIsLock(isLock);
                                        linkInfo.setCreater(user.getDisplayName());
                                        linkInfo.setCreateTime(new Date());
                                        long count = rtChangeLockInfoMapper.count(linkInfo);
                                        if (count > 0) {
                                            rtChangeLockInfoMapper.deleteBy(linkInfo);
                                        }
                                        lockInfoDOS.add(linkInfo);
                                    }
                                }
                                if (CollUtil.isNotEmpty(associatedbomDOS)) {
                                    for (RtChangeaffectAssociatedbomDO associatedbomDO : associatedbomDOS) {
                                        String partNumber = associatedbomDO.getPartnumber();
                                        String productCode = associatedbomDO.getCode();
                                        String materialCode = associatedbomDO.getMaterialCode();
                                        RtChangeLockInfoDO linkInfo = new RtChangeLockInfoDO();
                                        linkInfo.setProductCode(productCode);
                                        linkInfo.setChangeNo(changeNo);
                                        linkInfo.setPartNumber(partNumber);
                                        linkInfo.setMaterialCode(materialCode);
                                        linkInfo.setLocker(user.getDisplayName());
                                        linkInfo.setLockTime(new Date());
                                        linkInfo.setIsLock(isLock);
                                        linkInfo.setCreater(user.getDisplayName());
                                        linkInfo.setCreateTime(new Date());
                                        long count = rtChangeLockInfoMapper.count(linkInfo);
                                        if (count > 0) {
                                            rtChangeLockInfoMapper.deleteBy(linkInfo);
                                        }
                                        lockInfoDOS.add(linkInfo);
                                    }
                                }
                                if (CollUtil.isNotEmpty(lockInfoDOS))
                                    rtChangeLockInfoMapper.insertBatch(lockInfoDOS);
                            }
                        }
                        //解锁订单
                        if ("1".equals(isLock)){
                            RtChangeLockInfoDO infoDO = new RtChangeLockInfoDO();
                            infoDO.setChangeNo(changeNo);
                            infoDO.setIsLock("0");
                            rtChangeLockInfoMapper.deleteBy(infoDO);
                            UserDO user = TyAccountContext.getUser();
                            rtChangeLockInfoMapper.updateByInfo(changeNo, "2", "1", user.getDisplayName(), new Date());
                        }
                    }else if(changeNo.contains("RTTJJG")){
                        //解锁订单
                        if ("1".equals(isLock)){
                            RtChangeLockInfoDO infoDO = new RtChangeLockInfoDO();
                            infoDO.setChangeNo(changeNo);
                            infoDO.setIsLock("0");
                            rtChangeLockInfoMapper.deleteBy(infoDO);
                            UserDO user = TyAccountContext.getUser();
                            rtChangeLockInfoMapper.updateByInfo(changeNo, "2", "1", user.getDisplayName(), new Date());
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("解锁成品码下单异常", e);
            throw RTErrorCodeEnum.CUSTOM_EXCEPTION.getException("解锁成品码下单异常：" + e.getMessage());
        }
    }
}